<?php
	class Tabla{
		public $db=null;
		private $mitabla , $campos = array() , $totalcampos=0 , $poscampo=0 ,$lins=0,$registro, $esnuevo=0, $contenido = array();
		private $campo , $valores;
		var $releer,$row = array();
		function __construct($tabla){
			include_once('db.php');
			$this->db = new db();
			$this->db->query('DESC '.$tabla);
			$this->totalcampos = $this->db->rows();
			if($this->totalcampos === false){
				echo '<strong><h2 style="background-color:#567865;">Tabla Desconocida</h2></strong>';
			}
			else{
				$this->totalcampos--;
				
				for($x=0; $x<=$this->totalcampos; $x++ ){
					$camp = $this->db->fetch();
					$this->campos[$x] = $camp[0]; 
					echo $this->campos[$x].' ';
					
				}
			}
			
			$this->mitabla = $tabla;
		}
		public function pocisioncampo($elcampo){
			for($x=0; $x<=$this->totalcampos; $x++ ){
				
				if($this->campos[$x]===$elcampo){
					$this->poscampo = $x;
				}
			}
		}
		public function seleccionar($nombre , $campo , $selec , $cond='id<>0'){
					
			$select ='<select name="'.$nombre.'" id="'.$nombre.'"><option value=" "></option>';
			$sintaxis = "SELECT * FROM ".$this->mitabla." WHERE ".$cond; 
			$this->pocisioncampo($campo);
			$this->db->query($sintaxis);
			$lin = $this->db->rows();
			
			for($x=1; $x<=$lin; $x++)
			{
				$datos = $this->db->fetch();
				$sel='';
				
				if($datos[0] == $selec){ $sel=' selected="selected" ' ;}
				$select = $select.'<option value="'.$datos[0].'"'.$sel.'>'.$datos[$this->poscampo].'</option>';
							
				
			}
			
			$select = $select.'</select>';
			
			return $select;
						
				
		}
		
		
		
		
		
		
		
		
		
		
		public function buscarkey($key){
			$sin = 'SELECT * FROM '.$this->mitabla.' WHERE id='.$key;
			echo $sin;
			if($this->db->query($sin)){
				$this->registro = $this->db->fetch();  
				return true;
			}
			else{
				return false;
			}
		}
		public function buscarcondicion($cond){
			$sin ='SELECT * FROM '.$this->mitabla.' WHERE '.$cond;
			$this->db->query($sin);
			$this->lins = $this->db->rows();
			if($this->lins>0){
				$this->row =$this->db->fetch();
			}
			return $this->db->rows() > 0;
						
		}
		public function get($campo){
			
			for($x=1; $x<=$this->lins; $x++){
				$datos = $this->row;
				return $datos[$campo];
			}
			
		}
		
		public function obtenercampo($elcampo){
			
			for($x=0; $x<=$this->totalcampos; $x++){
				$this->contenido[$x] = $this->registro[$x];
				 				
				if($this->campos[$x]==$elcampo)
					return ($this->contenido[$x]);
			
			}
		}
		public function nuevo(){
			return $this->esnuevo = 1;
		}
		public function update($campos , $id){
			
			foreach($campos as $campo=>$contenido){
				if($campo != 'id'){
					$sin ='SELECT '.$campo.' FROM '.$this->mitabla.' WHERE id='.$id;
					$this->db->query($sin);
					$dt = $this->db->fetch();
					if($dt[0]!=$contenido){
						$syn = 'UPDATE '.$this->mitabla.' SET '.$campo.'="'.$contenido.'" WHERE id='.$id;
						$this->db->query($syn);
						echo $syn;
					}
				}
			}
		}
		public function colocar($campo , $valor){
			$this->campo = $this->campo.$campo.',';
			$this->valores = $this->valores.'"'.$valor.'",';
		}
		public function renombrar($campo1,$pk){
			$this->db->query('SELECT '.$campo1.' FROM '.$this->mitabla.' WHERE id='.$pk);
			$campo = $this->db->fetch();
			return $campo[0];
		}
		public function salvar(){
			
			if($this->esnuevo==1){
				$sql = 'INSERT INTO '.$this->mitabla.'('.$this->campo.') VALUES('.$this->valores.');';
				$sql = str_replace(',)' , ')' , $sql);
				$this->db->query($sql);
				//echo $sql;
			}
		}
		
	}
?>